home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / cbibcode.arc / LOCK.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-05  |  1.1 KB  |  45 lines

  1. /* lock.c, from page 361 of Turbo C Bible */
  2. #include <stdio.h>
  3. #include <io.h>
  4. main ()
  5. {
  6.    long curpos;
  7.    int filehandle;
  8.    char filename [80], buffer [80];
  9.    printf ("Eenter name of file to test with:");
  10.    gets (filename);
  11.    if ((filehandle = open (filename, O_RDONLY)) == -1)
  12.    {
  13.         perror ("open failed");
  14.         exit (1);
  15.    }
  16.                    /* Read 80 characters from the file */
  17.    if (read (filehandle, buffer, 80) == -1)
  18.    {
  19.         perror ("read error");
  20.         exit (1);
  21.    }
  22.                    /* Get and save current position    */
  23.    curpos = tell (filehandle);
  24.                    /* Now lock 80 bytes from the beginning
  25.                     of the file */
  26.    if (lock (filehandle, OL, curpos) == -1)
  27.    {
  28.         perror ("locking failed");
  29.    }
  30.    else
  31.    {
  32.     printf ("First %ld bytes of file %s locked\n", curpos, filename);
  33.                    /* In an actual program, you would make
  34.                 changes and write these bytes back to
  35.                 the file before unlocking. */
  36.     if (unlock (filehandle,  0L, curpos) == -1)
  37.         {
  38.              perror ("unlocking failed");
  39.         }
  40.         else
  41.         {
  42.              printf ("File unlocked\n");
  43.         }
  44.    }
  45. }